import {
    GETDATA,
    C,
    GetLists
} from '../utils/R'
import {
    cloneDeep,
    truncate,
    isUndefined
} from 'lodash'

export default {
    namespace: 'rightTopPage',
    state: {
        arr: [], //大屏数据存放
        njlist: [], //年级列表数据存放
    },
    subscriptions: {
        setup({
            dispatch,
            history
        }) {
            history.listen(({
                pathname
            }) => {
                if (pathname === '/one') {
                    dispatch({
                        type: 'getArr'
                    })
                    dispatch({
                        type: 'getNjlist'
                    })
                }
            })
        }
    },
    effects: {
        * getArr({
            payload
        }, {
            call,
            put
        }) {
            let {
                data
            } = yield call(GETDATA, C, {}, {
                nj: isUndefined(payload) ? "18春" : payload //默认为18年春
            })
            if (data) {
                let dataObj = {
                    data: [],
                    njlist: []
                }
                data.map((v, i) => {
                    dataObj.data.push(v.sums)
                    dataObj.njlist.push(truncate(v.zym, {
                        'length': 6,
                        'omission': '..'
                    }))
                })

                console.log(dataObj)

                yield put({
                    type: 'setArr',
                    payload: dataObj
                })
            }
        },


        * getNjlist({
            payload
        }, {
            call,
            put
        }) {
            let {
                data
            } = yield call(GetLists)
            if (data) {
                yield put({
                    type: 'setNjlist',
                    payload: data
                })
            }
        }
    },
    reducers: {
        setArr(state, action) {
            let newState = cloneDeep(state)
            newState.arr = action.payload
            return newState
        },
        setNjlist(state, action) {
            let newState = cloneDeep(state)
            newState.njlist = action.payload
            return newState
        }
    }
}